package com.bookmanger.server.controller;

import com.bookmanger.common.result.Result;
import com.bookmanger.pojo.DTO.BorrowDTO;
import com.bookmanger.pojo.Entity.BorrowEntity;
import com.bookmanger.pojo.VO.BorrowBook4ManagerVO;
import com.bookmanger.server.service.BorrowService;
import com.bookmanger.server.service.RankingService;
import com.bookmanger.server.service.impl.RankingServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/borrow")
@Slf4j
public class BorrowController {

    @Autowired
    private BorrowService borrowService;


    /**
     * 借阅图书（读者提交借阅请求）
     * @param borrowDTO 包含读者ID和ISBN ID
     * @return 借阅结果
     */
    @PostMapping("/add")
    public Result borrowBook(@RequestBody BorrowDTO borrowDTO) {
        log.info("借阅图书: {}", borrowDTO);
        return borrowService.borrowBook(borrowDTO) ? Result.success("借阅请求提交成功") : Result.error("借阅请求提交失败");
    }
    
    /**
     * 批准借阅（管理员操作）
     * @param borrowId 借阅记录ID
     * @param managerId 管理员ID
     * @return 批准结果
     */
    @PutMapping("/approve")
    public Result approveBorrow(@RequestParam Integer borrowId, @RequestParam Integer managerId) {
        return borrowService.approveBorrow(borrowId, managerId);
    }

    /**
     * 归还图书（读者提交归还请求）
     * @param borrowId 借阅记录ID
     * @return 归还结果
     */
    @PutMapping("/return")
    public Result returnBook(@RequestParam Integer borrowId) {
        log.info("归还图书，借阅记录ID: {}", borrowId);
        return borrowService.returnBook(borrowId) ? Result.success("归还请求提交成功") : Result.error("归还请求提交失败");
    }
    
    /**
     * 批准归还（管理员操作）
     * @param borrowId 借阅记录ID
     * @param managerId 管理员ID
     * @return 批准结果
     */
    @PutMapping("/approveReturn")
    public Result approveReturnBook(@RequestParam Integer borrowId, @RequestParam Integer managerId) {
        log.info("批准归还，借阅记录ID: {}，管理员ID: {}", borrowId, managerId);
        return borrowService.approveReturnBook(borrowId, managerId) ? Result.success("归还批准成功") : Result.error("归还批准失败");
    }

    /**
     * 申报图书损坏或丢失
     * @param borrowId 借阅记录ID
     * @param reason 损坏或丢失原因
     * @return 处理结果
     */
    @PutMapping("/damagedOrLost")
    public Result handleDamagedOrLostBook(@RequestParam Integer borrowId, @RequestParam String reason) {
        log.info("处理图书损坏或丢失，借阅记录ID: {}，原因: {}", borrowId, reason);
        return borrowService.handleDamagedOrLostBook(borrowId, reason) ?
                Result.success("图书损坏/丢失处理成功，已通知读者") : Result.error("处理失败");
    }
    
    /**
     * 查询所有未审批的借书申请
     * @return 借书申请列表
     */
    @GetMapping("/pendingBorrows")
    public Result getPendingBorrowApplications(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer pageSize) {
        // 参数校验
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<BorrowBook4ManagerVO> borrowEntities = borrowService.getPendingBorrowApplicationsWithDetails(page, pageSize);
        return Result.success(borrowEntities);
    }
    
    /**
     * 查询所有已审批的借书申请
     * @return 借书申请列表
     */
    @GetMapping("/approvedBorrows")
    public Result getApprovedBorrowApplications(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pageSize) {
        // 参数校验
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<BorrowBook4ManagerVO> borrowEntities = borrowService.getApprovedBorrowApplicationsWithDetails(page, pageSize);
        return Result.success(borrowEntities);
    }
    
    /**
     * 查询所有未审批的还书申请
     * @return 还书申请列表
     */
    @GetMapping("/pendingReturns")
    public Result getPendingReturnApplications(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer pageSize) {
        // 参数校验
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<BorrowBook4ManagerVO> borrowEntities = borrowService.getPendingReturnApplicationsWithDetails(page, pageSize);
        return Result.success(borrowEntities);
    }
    
    /**
     * 查询所有已审批的还书申请
     * @return 还书申请列表
     */
    @GetMapping("/approvedReturns")
    public Result getApprovedReturnApplications(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pageSize) {
        // 参数校验
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<BorrowBook4ManagerVO> borrowEntities = borrowService.getApprovedReturnApplicationsWithDetails(page, pageSize);
        return Result.success(borrowEntities);
    }
    
    /**
     * 根据用户ID或名称模糊查询借阅记录
     * @param keyword 用户ID或名称关键字
     * @param page 页码
     * @param pageSize 每页大小
     * @return 借阅记录列表
     */
    @GetMapping("/search")
    public Result getBorrowRecordsByUserIdOrName(@RequestParam String keyword,
                                                 @RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        // 参数校验
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<BorrowBook4ManagerVO> borrowRecords = borrowService.getBorrowRecordsByUserIdOrName(keyword, page, pageSize);
        return Result.success(borrowRecords);
    }
}